package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gillion.grule.client.core.DubboConfig;

import com.google.common.collect.Lists;
import com.rules.admin.dao.DubboModelDao;
import com.rules.admin.entity.DubboModel;
import com.rules.admin.service.DubboService;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class DubboServiceImpl extends ServiceImpl<DubboModelDao, DubboModel> implements DubboService {
   public IPage<DubboModel> queryPage(DubboModel dubboModel, IPage<DubboModel> page) {
      LambdaQueryWrapper<DubboModel> queryWrapper = new LambdaQueryWrapper<>();
      if (dubboModel.getProjectId() != null && !"".equals(dubboModel.getProjectId())) {
         queryWrapper.eq(DubboModel::getProjectId,dubboModel.getProjectId());
      }
      if (dubboModel.getDubboName() != null && !"".equals(dubboModel.getDubboName())) {
         queryWrapper.eq(DubboModel::getDubboName,dubboModel.getDubboName());
      }
      return this.page(page, queryWrapper);
   }

   public List<DubboConfig> selectListByProjectCode(String projectCode) {
      LambdaQueryWrapper<DubboModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DubboModel::getProjectCode,projectCode);
      List<DubboModel> dubboModelList = this.list(queryWrapper);


      List<DubboConfig> dubboConfigsList = Lists.newArrayList();
      Iterator iterator = dubboModelList.iterator();

      while(iterator.hasNext()) {
         DubboModel dubboModel = (DubboModel)iterator.next();
         DubboConfig dubboConfig = new DubboConfig();
         dubboConfig.setDubboName(dubboModel.getDubboName());
         dubboConfig.setClassName(dubboModel.getClassName());
         dubboConfig.setMethodName(dubboModel.getMethodName());
         dubboConfig.setParams(dubboModel.getParams());
         dubboConfigsList.add(dubboConfig);
      }

      return dubboConfigsList;
   }

   public List<DubboModel> getListByProjectCode(String projectId) {
      LambdaQueryWrapper<DubboModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DubboModel::getProjectId,projectId);
      return this.list(queryWrapper);
   }
}
